home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / ham84.c < prev    next >
Text File  |  2000-05-18  |  2KB  |  70 lines

  1. /******************************************************************
  2. *
  3. *    HAM84 Version 45G
  4. *
  5. ******************************************************************
  6. *
  7. *  Hamming 8,4 Decoder - can correct 1 out of seven bits
  8. *   and can detect up to two errors.
  9. *
  10. * INPUT:
  11. *  INPUT  - Seven bit data word ,4 bits parameter and
  12. *           4 bits parity information
  13. * OUTPUTS:
  14. *  OUTPUT - 4 corrected parameter bits
  15. *  ERRCNT - Sums errors detected by Hamming code
  16. *
  17. * This subroutine is entered with an eight bit word in INPUT.
  18. *  The 8th bit is parity and is stripped off.  The remaining 7 bits 
  19. *  address the hamming 8,4 table and the output    OUTPUT from the table
  20. *  gives the 4 bits of corrected data.  If bit 4 is set, no error was
  21. *  detected.  ERRCNT is the number of errors counted.
  22. */
  23.  
  24. int dactab[128] = {
  25.     16,0,0,3,0,5,14,7,0,9,14,11,
  26.     14,13,30,14,0,9,2,7,4,7,7,23,9,
  27.     25,10,9,12,9,14,7,0,5,2,11,5,21,
  28.     6,5,8,11,11,27,12,5,14,11,2,1,18,
  29.     2,12,5,2,7,12,9,2,11,28,12,12,15,
  30.     0,3,3,19,4,13,6,3,8,13,10,3,13,29,
  31.     14,13,4,1,10,3,20,4,4,7,10,9,26,
  32.     10,4,13,10,15,8,1,6,3,6,5,22,6,24,
  33.     8,8,11,8,13,6,15,1,17,2,1,4,1,6,
  34.     15,8,1,10,15,12,15,15,31
  35. };
  36. void ham84( input, output, errcnt )
  37. int input, *output, *errcnt;
  38. {
  39. int i, j, parity;
  40.  
  41.  
  42.  
  43. /*  Determine parity of input word     */
  44. parity = input & 255;
  45. parity = parity ^ parity/16;
  46. parity = parity ^ parity/4;
  47. parity = parity ^ parity/2;
  48. parity = parity & 1;
  49.  
  50. i = dactab[ input&127];
  51. *output = i&15;
  52. j = i&16;
  53.  
  54. if(j!=0) {
  55. /*          No errors detected in seven bits    */
  56.     if(parity!=0) *errcnt++;
  57. }
  58. else    {
  59. /*          One or two errors detected    */
  60.     *errcnt++;
  61.     if(parity==0) {
  62. /*             Two errors detected    */
  63.         *errcnt++;
  64.         *output = -1;
  65.     }
  66. }
  67.  
  68.  
  69. }
  70.